<template>
  <div class="login">
    <h1>电影后台管理</h1>
    <span style="color: red;width: 100%; text-align: center;">
      (测试账号：用户名：fanchao,密码：fanchao)
    </span>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名：" prop="username">
        <el-input v-model="ruleForm.username" prefix-icon="el-icon-s-custom" autocomplete="off" clearable></el-input>
      </el-form-item>
      <el-form-item label="密码：" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" prefix-icon="el-icon-s-custom" autocomplete="off" clearable></el-input>
      </el-form-item>
      <el-form-item class="button">
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import Vue from 'vue/dist/vue.esm.js'

import { getUserInfo } from '../../utils/api'
import { mapMutations } from 'vuex'

export default {
  name: 'login',
  data () {
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback()
      }
    }
    let username = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {
        callback();
      }
    }

    return{
      ruleForm: {
        username: '',
        pass: '',
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        username: [
          { validator: username, trigger: 'blur' }
        ]
      }
    };
  },
  components: {
  },
  methods: {
    ...mapMutations ([
      'setUsername'
    ]),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          getUserInfo(this.ruleForm).then(res => {
            if (res.success) {
              this.$router.push('/admin/movieList')
              this.setUsername(res.user)
            } else {
              this.$alert(res.message, '提示', {
                confirmButtonText: '确定',
                closeOnClickModal: true,
                center: true,
                callback: action => {
                }
              })
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$router.push('/')
    }
  }
}
</script>
<style lang="scss" scoped>
  .login{
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    >h1{
      text-indent: 100px;
      margin-bottom: 50px;
    }
    .demo-ruleForm{
      width: 500px;
      height: 300px;
    }
  }
</style>
